﻿using System;
using System.Collections.Generic;
using System.Data.Objects;
using System.Data.Objects.DataClasses;
using System.Linq;

namespace DataGenerator
{
    /// <summary>
    /// Extension methods.
    /// </summary>
    public static class Extensions
    {
        /// <summary>
        /// Random number generator.
        /// </summary>
        private static readonly Random Random = new Random();

        /// <summary>
        /// Adds the range.
        /// </summary>
        /// <typeparam name="T">Type.</typeparam>
        /// <param name="collection">The collection.</param>
        /// <param name="addItems">The add items.</param>
        public static void AddRange<T>(this EntityCollection<T> collection, IEnumerable<T> addItems) where T:class 
        {
            foreach (var addItem in addItems)
            {
                collection.Add(addItem);
            }
        }

        /// <summary>
        /// Adds the objects.
        /// </summary>
        /// <typeparam name="T">Type.</typeparam>
        /// <param name="collection">The collection.</param>
        /// <param name="addItems">The add items.</param>
        public static void AddObjects<T>(this ObjectSet<T> collection, IEnumerable<T> addItems) where T : class
        {
            foreach (var addItem in addItems)
            {
                collection.AddObject(addItem);
            }
        }

        /// <summary>
        /// Gets the random item.
        /// </summary>
        /// <typeparam name="T">Type.</typeparam>
        /// <param name="items">The items.</param>
        /// <returns>Random item.</returns>
        public static T GetRandomItem<T>(this IEnumerable<T> items)
        {
            var itemList = items.ToList();
            if (itemList.Count == 0)
            {
                return default(T);
            }

            var i = Random.Next(itemList.Count);
            return itemList[i];
        }
    }
}
